package com.tadpole.music.holder.me;

import android.app.Activity;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;


import com.google.android.material.tabs.TabLayout;
import com.tadpole.music.R;
import com.tadpole.music.adapter.base.BaseAdapter;
import com.tadpole.music.adapter.base.TabAdapter;
import com.tadpole.music.bean.me.MoNiErrorBean;
import com.tadpole.music.holder.base.BaseViewHolder;
import com.tadpole.music.view.fragment.me.MoNiV2Fragment;
import com.tadpole.music.view.fragment.me.ZhenTiV2Fragment;
import com.tadpole.music.view.widget.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created By 孙岩
 * on 2020/12/26
 */
public class AnswerStatisticsV2ViewHolder extends BaseViewHolder {
    private Activity activity;
    private View itemView;
    private BaseAdapter.OnChildClickListener onChildClickListener;
    //tabLayout
    private TabLayout tabLayout;
    //viewPager
    private NoScrollViewPager viewPager;
    private TabAdapter tabAdapter;
    //tab集合
    private List<String> tabList = new ArrayList<>();
    //viewpager绑定的fragment的集合
    private List<Fragment> fragmentList = new ArrayList<>();
    private FragmentManager fragmentManager;

    private TextView tvTitle;
    //学生id
    private int student_id;
    //学生头像
    private String student_avatar;
    //学生昵称
    private String student_nickname;
    //数据源
    private List<MoNiErrorBean.DataBean> list;

    public AnswerStatisticsV2ViewHolder(Activity activity, View itemView, BaseAdapter.OnChildClickListener onChildClickListener, FragmentManager fragmentManager, int student_id, String student_avatar, String student_nickname, List<MoNiErrorBean.DataBean> list) {
        super(itemView);
        this.activity = activity;
        this.itemView = itemView;
        this.onChildClickListener = onChildClickListener;
        this.fragmentManager = fragmentManager;
        this.student_id = student_id;
        this.student_avatar = student_avatar;
        this.student_nickname = student_nickname;
        this.list = list;
    }

    @Override
    public void init(int position) {
        tabLayout = itemView.findViewById(R.id.tabLayout);
        viewPager = itemView.findViewById(R.id.viewPager);
        tvTitle = itemView.findViewById(R.id.tvTitle);
        tvTitle.setText(list.get(position).getName());
        initTab(position);

    }

    //初始化tab
    private void initTab(int position) {
        if (viewPager != null) {

        tabList.add("模拟训练");
        tabList.add("实战真题");
        fragmentList.add(MoNiV2Fragment.newInstance(list.get(position), student_id + "", student_avatar, student_nickname));
        fragmentList.add(ZhenTiV2Fragment.newInstance(list.get(position).getRank_id() + "", student_id + ""));

        tabAdapter = new TabAdapter(fragmentList, tabList, fragmentManager, activity);
//        viewPager.setCurrentItem(0);
        viewPager.setId(position + 1);
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setAdapter(tabAdapter);
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(tabAdapter.getTabView(i));
            }

            if (i == 0) {
                //设置默认进来第一个是选中状态
                TabLayout.Tab a = tabLayout.getTabAt(0);
                View view1 = a.getCustomView();
                if (null != view1 && view1 instanceof TextView) {
                    ((TextView) view1).setTextSize(16);
                    ((TextView) view1).setTextColor(activity.getResources().getColor(R.color.color_33));
                }
            } else {
                //设置默认进来第二个是不选中状态
                TabLayout.Tab b = tabLayout.getTabAt(1);
                View view2 = b.getCustomView();
                if (null != view2 && view2 instanceof TextView) {
                    ((TextView) view2).setTextSize(13);
                    ((TextView) view2).setTextColor(activity.getResources().getColor(R.color.color_65));
                }
            }
        }


        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        //切换导航菜单时候的监听
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (viewPager != null) {
                    viewPager.setCurrentItem(tab.getPosition(), true);
                    View view = tab.getCustomView();
                    if (null != view && view instanceof TextView) {
                        ((TextView) view).setTextSize(16);
                        ((TextView) view).setTextColor(activity.getResources().getColor(R.color.color_33));
                        ((TextView) view).setTextAppearance(activity, R.style.TabLayoutTextStyle);
                    }
                }


            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if (viewPager != null) {
                    View view = tab.getCustomView();
                    if (null != view && view instanceof TextView) {
                        ((TextView) view).setTextSize(13);
                        ((TextView) view).setTextColor(activity.getResources().getColor(R.color.color_65));
                    }
                }
            }


            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        }
    }
}
